package frame.laxcen.response;

import frame.laxcen.BaseFrame;
import frame.laxcen.exception.IllegalFrameFormatException;
import frame.laxcen.util.FrameReader;
import frame.laxcen.util.FrameUtil;

import java.util.HashMap;
import java.util.Map;

// 读写器对读取 6C 标签命令的返回值
public class Response_0010_10 extends BaseFrame implements IResponse {

    public Response_0010_10(BaseFrame baseFrame) throws IllegalFrameFormatException {

        initialize(baseFrame.FrameBytes);
    }

    public Response_0010_10(byte[] bytes) throws IllegalFrameFormatException {

        initialize(bytes);
    }

    // 根据返回帧的类型路由到各处理方法中进行内容解析
    @Override
    public Map<String, String> explainToMap() throws IllegalFrameFormatException {

        if(CW.IsUpload) {

            if(CW.MID == 0) return explainRFIDMessageToMap();

            if(CW.MID == 1) return explainStopResultToMap();

            throw new IllegalFrameFormatException("Not an legal 0010_10 Frame.");

        } else {

            return explainConfigResultToMap();
        }
    }

    // 解析读写器对读操作的配置结果
    private Map<String, String> explainConfigResultToMap() throws IllegalFrameFormatException {

        Map<String, String> result = new HashMap<>();

        try {

            FrameReader reader = new FrameReader(this);

            // 读操作配置结果

            switch (reader.readU8()) {

                case 0: result.put("读操作配置结果", "配置成功"); break;
                case 1: result.put("读操作配置结果", "天线端口参数错误"); break;
                case 2: result.put("读操作配置结果", "选择读取参数错误"); break;
                case 3: result.put("读操作配置结果", "TID 读取参数错误"); break;
                case 4: result.put("读操作配置结果", "用户数据区读取参数错误"); break;
                case 5: result.put("读操作配置结果", "保留区读取参数错误"); break;
                case 6: result.put("读操作配置结果", "其他参数错误"); break;
            }

        } catch (Exception e) {

            e.printStackTrace();

            throw new IllegalFrameFormatException(e.getMessage());
        }

        return result;
    }

    // 解析返回的 6C 标签数据信息
    private Map<String, String> explainRFIDMessageToMap() throws IllegalFrameFormatException {

        Map<String, String> result = new HashMap<>();

        try {

            FrameReader reader = new FrameReader(this);

            // 标签 ECP 码

            result.put("标签ECP码", FrameUtil.toHexString(reader.readU8s()));

            // 标签 PC

            result.put("标签PC", String.valueOf(reader.readU16()));

            // 天线 ID

            result.put("天线ID", Integer.toBinaryString(reader.readU8()));

            for(int i = 0; i < 5 && reader.hasNext(); ++i) {

                // RSSI

                reader.readP8((byte) 0x01, (b) -> result.put("RSSI", Integer.toUnsignedString(b)));

                // 标签数据读取结果

                reader.readP8((byte) 0x02, (b) -> {

                    switch(b) {

                        case 0: result.put("标签数据读取结果", "读取成功"); break;
                        case 1: result.put("标签数据读取结果", "标签无响应"); break;
                        case 2: result.put("标签数据读取结果", "CRC错误"); break;
                        case 3: result.put("标签数据读取结果", "数据区被锁定"); break;
                        case 4: result.put("标签数据读取结果", "数据区溢出"); break;
                        case 5: result.put("标签数据读取结果", "访问密码错误"); break;
                        case 6: result.put("标签数据读取结果", "其他标签错误"); break;
                        case 7: result.put("标签数据读取结果", "其他读写器错误"); break;
                    }
                });

                // 标签 TID 数据

                reader.readP8s((byte) 0x03, (ss) -> result.put("标签TID数据", FrameUtil.toHexString(ss)));

                // 标签用户数据区数据

                reader.readP8s((byte) 0x04, (ss) -> result.put("标签用户数据区数据", FrameUtil.toHexString(ss)));

                // 标签保留区数据

                reader.readP8s((byte) 0x05, (ss) -> result.put("标签保留区数据", FrameUtil.toHexString(ss)));
            }

        } catch (Exception e) {

            e.printStackTrace();

            throw new IllegalFrameFormatException(e.toString());
        }

        return result;
    }

    // 解析结束读取操作的原因
    private Map<String, String> explainStopResultToMap() throws IllegalFrameFormatException {

        Map<String, String> result = new HashMap<>();

        try {

            FrameReader reader = new FrameReader(this);

            // 读取结束原因

            switch(reader.readU8()) {

                case 0: result.put("读卡结束原因", "单次操作完成"); break;
                case 1: result.put("读卡结束原因", "收到停止指令"); break;
                case 2: result.put("读卡结束原因", "硬件故障导致读卡中断"); break;
            }

        } catch (Exception e) {

            e.printStackTrace();

            throw new IllegalFrameFormatException(e.getMessage());
        }

        return result;
    }
}
